home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / gnudiff / C / Context < prev    next >
Text File  |  1991-09-16  |  13KB  |  433 lines

  1. /* Context-format output routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21. #include "regex.h"
  22.  
  23. static void print_context_label (const char *, struct file_data *,
  24.                  const char *, int);
  25. static void print_context_number_range (struct file_data *, int, int);
  26. static void pr_context_hunk (struct change *);
  27. static void print_unidiff_number_range (struct file_data *, int, int);
  28. static void pr_unidiff_hunk (struct change *);
  29. static struct change *find_hunk (struct change *);
  30. static void mark_ignorable (struct change *);
  31. static void find_function (struct file_data *, int, char **, int *);
  32.  
  33. /* Last place find_function started searching from.  */
  34. static int find_function_last_search;
  35.  
  36. /* The value find_function returned when it started searching there.  */
  37. static int find_function_last_match;
  38.  
  39. /* Print a label for a context diff, with a file name and date or a label.  */
  40.  
  41. static void print_context_label (const char *mark, struct file_data *inf,
  42.                  const char *label, int pad_len)
  43. {
  44.   if (label)
  45.     fprintf (outfile, "%s %s\n", mark, label);
  46.   else if (inf->timestamp[0])
  47.     fprintf (outfile, "%s %-*s%s", mark, pad_len, inf->name, inf->timestamp);
  48.   else
  49.     /* Don't pretend that standard input is ancient.  */
  50.     fprintf (outfile, "%s %-*s\n", mark, pad_len, inf->name);
  51. }
  52.  
  53. /* Print a header for a context diff, with the file names and dates.  */
  54.  
  55. void print_context_header (struct file_data *inf, int unidiff_flag)
  56. {
  57.   int len0 = strlen(inf[0].name);
  58.   int len1 = strlen(inf[1].name);
  59.   int pad_len = (len0 < len1 ? len1 : len0);
  60.  
  61.   /* Pad the names to a tab stop (making sure to leave at least one space) */
  62.   pad_len = (pad_len + 8) & ~0x07;
  63.  
  64.   if (unidiff_flag)
  65.     {
  66.       print_context_label ("---", &inf[0], file_label[0], pad_len);
  67.       print_context_label ("+++", &inf[1], file_label[1], pad_len);
  68.     }
  69.   else
  70.     {
  71.       print_context_label ("***", &inf[0], file_label[0], pad_len);
  72.       print_context_label ("---", &inf[1], file_label[1], pad_len);
  73.     }
  74. }
  75.  
  76. /* Print an edit script in context format.  */
  77.  
  78. void print_context_script (struct change *script, int unidiff_flag)
  79. {
  80.   if (ignore_blank_lines_flag || ignore_regexp)
  81.     mark_ignorable (script);
  82.   else
  83.     {
  84.       struct change *e;
  85.       for (e = script; e; e = e->link)
  86.     e->ignore = 0;
  87.     }
  88.  
  89.   find_function_last_search = 0;
  90.   find_function_last_match = -1;
  91.  
  92.   if (unidiff_flag)
  93.     print_script (script, find_hunk, pr_unidiff_hunk);
  94.   else
  95.     print_script (script, find_hunk, pr_context_hunk);
  96. }
  97.  
  98. /* Print a pair of line numbers with a comma, translated for file FILE.
  99.    If the second number is not greater, use the first in place of it.
  100.  
  101.    Args A and B are internal line numbers.
  102.    We print the translated (real) line numbers.  */
  103.  
  104. static void print_context_number_range (struct file_data *file, int a, int b)
  105. {
  106.   int trans_a, trans_b;
  107.   translate_range (file, a, b, &trans_a, &trans_b);
  108.  
  109.   /* Note: we can have B < A in the case of a range of no lines.
  110.      In this case, we should print the line number before the range,
  111.      which is B.  */
  112.   if (trans_b > trans_a)
  113.     fprintf (outfile, "%d,%d", trans_a, trans_b);
  114.   else
  115.     fprintf (outfile, "%d", trans_b);
  116. }
  117.  
  118. /* Print a portion of an edit script in context format.
  119.    HUNK is the beginning of the portion to be printed.
  120.    The end is marked by a `link' that has been nulled out.
  121.  
  122.    Prints out lines from both files, and precedes each
  123.    line with the appropriate flag-character.  */
  124.  
  125. static void pr_context_hunk (struct change *hunk)
  126. {
  127.   int first0, last0, first1, last1, show_from, show_to, i;
  128.   struct change *next;
  129.   char *prefix;
  130.   char *function;
  131.   int function_length;
  132.  
  133.   /* Determine range of line numbers involved in each file.  */
  134.  
  135.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  136.  
  137.   if (!show_from && !show_to)
  138.     return;
  139.  
  140.   /* Include a context's width before and after.  */
  141.  
  142.   first0 = max (first0 - context, 0);
  143.   first1 = max (first1 - context, 0);
  144.   last0 = min (last0 + context, files[0].buffered_lines - 1);
  145.   last1 = min (last1 + context, files[1].buffered_lines - 1);
  146.  
  147.   /* If desired, find the preceding function definition line in file 0.  */
  148.   function = 0;
  149.   if (function_regexp)
  150.     find_function (&files[0], first0, &function, &function_length);
  151.  
  152.   /* If we looked for and found a function this is part of,
  153.      include its name in the header of the diff section.  */
  154.   fprintf (outfile, "***************");
  155.  
  156.   if (function)
  157.     {
  158.       fprintf (outfile, " ");
  159.       fwrite (function, 1, min (function_length - 1, 40), outfile);
  160.     }
  161.  
  162.   fprintf (outfile, "\n*** ");
  163.   print_context_number_range (&files[0], first0, last0);
  164.   fprintf (outfile, " ****\n");
  165.  
  166.   if (show_from)
  167.     {
  168.       next = hunk;
  169.  
  170.       for (i = first0; i <= last0; i++)
  171.     {
  172.       /* Skip past changes that apply (in file 0)
  173.          only to lines before line I.  */
  174.  
  175.       while (next && next->line0 + next->deleted <= i)
  176.         next = next->link;
  177.  
  178.       /* Compute the marking for line I.  */
  179.  
  180.       prefix = " ";
  181.       if (next && next->line0 <= i)
  182.         /* The change NEXT covers this line.
  183.            If lines were inserted here in file 1, this is "changed".
  184.            Otherwise it is "deleted".  */
  185.         prefix = (next->inserted > 0 ? "!" : "-");
  186.  
  187.       print_1_line (prefix, &files[0].linbuf[i]);
  188.     }
  189.     }
  190.  
  191.   fprintf (outfile, "--- ");
  192.   print_context_number_range (&files[1], first1, last1);
  193.   fprintf (outfile, " ----\n");
  194.  
  195.   if (show_to)
  196.     {
  197.       next = hunk;
  198.  
  199.       for (i = first1; i <= last1; i++)
  200.     {
  201.       /* Skip past changes that apply (in file 1)
  202.          only to lines before line I.  */
  203.  
  204.       while (next && next->line1 + next->inserted <= i)
  205.         next = next->link;
  206.  
  207.       /* Compute the marking for line I.  */
  208.  
  209.       prefix = " ";
  210.       if (next && next->line1 <= i)
  211.         /* The change NEXT covers this line.
  212.            If lines were deleted here in file 0, this is "changed".
  213.            Otherwise it is "inserted".  */
  214.         prefix = (next->deleted > 0 ? "!" : "+");
  215.  
  216.       print_1_line (prefix, &files[1].linbuf[i]);
  217.     }
  218.     }
  219. }
  220.  
  221. /* Print a pair of line numbers with a comma, translated for file FILE.
  222.    If the second number is smaller, use the first in place of it.
  223.    If the numbers are equal, print just one number.
  224.  
  225.    Args A and B are internal line numbers.
  226.    We print the translated (real) line numbers.  */
  227.  
  228. static void print_unidiff_number_range (struct file_data *file, int a, int b)
  229. {
  230.   int trans_a, trans_b;
  231.   translate_range (file, a, b, &trans_a, &trans_b);
  232.  
  233.   /* Note: we can have B < A in the case of a range of no lines.
  234.      In this case, we should print the line number before the range,
  235.      which is B.  */
  236.   if (trans_b <= trans_a)
  237.     fprintf (outfile, trans_b == trans_a ? "%d" : "%d,0", trans_b);
  238.   else
  239.     fprintf (outfile, "%d,%d", trans_a, trans_b - trans_a + 1);
  240. }
  241.  
  242. /* Print a portion of an edit script in unidiff format.
  243.    HUNK is the beginning of the portion to be printed.
  244.    The end is marked by a `link' that has been nulled out.
  245.  
  246.    Prints out lines from both files, and precedes each
  247.    line with the appropriate flag-character.  */
  248.  
  249. static void pr_unidiff_hunk (struct change *hunk)
  250. {
  251.   int first0, last0, first1, last1, show_from, show_to, i, j, k;
  252.   struct change *next;
  253.   char *function;
  254.   int function_length;
  255.  
  256.   /* Determine range of line numbers involved in each file.  */
  257.  
  258.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  259.  
  260.   if (!show_from && !show_to)
  261.     return;
  262.  
  263.   /* Include a context's width before and after.  */
  264.  
  265.   first0 = max (first0 - context, 0);
  266.   first1 = max (first1 - context, 0);
  267.   last0 = min (last0 + context, files[0].buffered_lines - 1);
  268.   last1 = min (last1 + context, files[1].buffered_lines - 1);
  269.  
  270.   /* If desired, find the preceding function definition line in file 0.  */
  271.   function = 0;
  272.   if (function_regexp)
  273.     find_function (&files[0], first0, &function, &function_length);
  274.  
  275.   /* If we looked for and found a function this is part of,
  276.      include its name in the header of the diff section.  */
  277.  
  278.   fprintf (outfile, "@@ -");
  279.   print_unidiff_number_range (&files[0], first0, last0);
  280.   fprintf (outfile, " +");
  281.   print_unidiff_number_range (&files[1], first1, last1);
  282.   fprintf (outfile, " @@");
  283.  
  284.   if (function)
  285.     {
  286.       putc (' ', outfile);
  287.       fwrite (function, 1, min (function_length - 1, 40), outfile);
  288.     }
  289.   putc ('\n', outfile);
  290.  
  291.   next = hunk;
  292.   i = first0;
  293.   j = first1;
  294.  
  295.   while (i <= last0 || j <= last1)
  296.     {
  297.  
  298.       /* If the line isn't a difference, output the context from file 0. */
  299.  
  300.       if (!next || i < next->line0)
  301.     {
  302.       putc (' ', outfile);
  303.       print_1_line ((char *)0, &files[0].linbuf[i++]);
  304.       j++;
  305.     }
  306.       else
  307.     {
  308.       /* For each difference, first output the deleted part. */
  309.  
  310.       k = next->deleted;
  311.       while (k--)
  312.         {
  313.           putc ('-', outfile);
  314.           print_1_line ((char *)0, &files[0].linbuf[i++]);
  315.         }
  316.  
  317.       /* Then output the inserted part. */
  318.  
  319.       k = next->inserted;
  320.       while (k--)
  321.         {
  322.           putc ('+', outfile);
  323.           print_1_line ((char *)0, &files[1].linbuf[j++]);
  324.         }
  325.  
  326.       /* We're done with this hunk, so on to the next! */
  327.  
  328.       next = next->link;
  329.     }
  330.     }
  331. }
  332.  
  333. /* Scan a (forward-ordered) edit script for the first place that at least
  334.    2*CONTEXT unchanged lines appear, and return a pointer
  335.    to the `struct change' for the last change before those lines.  */
  336.  
  337. static struct change *find_hunk (struct change *start)
  338. {
  339.   struct change *prev;
  340.   int top0, top1;
  341.   int thresh;
  342.  
  343.   do
  344.     {
  345.       /* Computer number of first line in each file beyond this changed.  */
  346.       top0 = start->line0 + start->deleted;
  347.       top1 = start->line1 + start->inserted;
  348.       prev = start;
  349.       start = start->link;
  350.       /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  351.      but only CONTEXT if one is ignorable.  */
  352.       thresh = ((prev->ignore || (start && start->ignore))
  353.         ? context
  354.         : 2 * context);
  355.       /* It is not supposed to matter which file we check in the end-test.
  356.      If it would matter, crash.  */
  357.       if (start && start->line0 - top0 != start->line1 - top1)
  358.     abort ();
  359.     } while (start
  360.          /* Keep going if less than THRESH lines
  361.         elapse before the affected line.  */
  362.          && start->line0 < top0 + thresh);
  363.  
  364.   return prev;
  365. }
  366.  
  367. /* Set the `ignore' flag properly in each change in SCRIPT.
  368.    It should be 1 if all the lines inserted or deleted in that change
  369.    are ignorable lines.  */
  370.  
  371. static void mark_ignorable (struct change *script)
  372. {
  373.   while (script)
  374.     {
  375.       struct change *next = script->link;
  376.       int first0, last0, first1, last1, deletes, inserts;
  377.  
  378.       /* Turn this change into a hunk: detach it from the others.  */
  379.       script->link = 0;
  380.  
  381.       /* Determine whether this change is ignorable.  */
  382.       analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
  383.       /* Reconnect the chain as before.  */
  384.       script->link = next;
  385.  
  386.       /* If the change is ignorable, mark it.  */
  387.       script->ignore = (!deletes && !inserts);
  388.  
  389.       /* Advance to the following change.  */
  390.       script = next;
  391.     }
  392. }
  393.  
  394. /* Find the last function-header line in FILE prior to line number LINENUM.
  395.    This is a line containing a match for the regexp in `function_regexp'.
  396.    Store the address of the line text into LINEP and the length of the
  397.    line into LENP.
  398.    Do not store anything if no function-header is found.  */
  399.  
  400. static void find_function (struct file_data *file, int linenum, char **linep, int *lenp)
  401. {
  402.   int i = linenum;
  403.   int last = find_function_last_search;
  404.   find_function_last_search = i;
  405.  
  406.   while (--i >= last)
  407.     {
  408.       /* See if this line is what we want.  */
  409.  
  410.       if (0 <= re_search (&function_regexp_compiled,
  411.               file->linbuf[i].text,
  412.               file->linbuf[i].length,
  413.               0, file->linbuf[i].length,
  414.               0))
  415.     {
  416.       *linep = file->linbuf[i].text;
  417.       *lenp = file->linbuf[i].length;
  418.       find_function_last_match = i;
  419.       return;
  420.     }
  421.     }
  422.   /* If we search back to where we started searching the previous time,
  423.      find the line we found last time.  */
  424.   if (find_function_last_match >= 0)
  425.     {
  426.       i = find_function_last_match;
  427.       *linep = file->linbuf[i].text;
  428.       *lenp = file->linbuf[i].length;
  429.       return;
  430.     }
  431.   return;
  432. }
  433.